package sg.edu.nus.iss.cats.action;

import javax.servlet.http.*;



/** WORKSHOP W06_CONTROLLER
  * Import struts action classes
  *********/
  
import org.apache.struts.action.*;
import org.apache.struts.actions.*;

import org.apache.commons.beanutils.*;
import sg.edu.nus.iss.cats.form.*;
import sg.edu.nus.iss.cats.model.*;
import sg.edu.nus.iss.cats.controller.*;
import sg.edu.nus.iss.cats.exception.*;


/** WORKSHOP W06_CONTROLLER
  * Extend from the org.apache.struts.actions.DispatchAction class
  *********/
public class CourseManageAction extends DispatchAction {

    /**
      * The view method gets a course_id, looks
      * up the course details, initializes the FormBean
      * for display by the courseForm.jsp
      */
      
      /** WORKSHOP W06_CONTROLLER
        * Throw the CourseApplnUpdateException, CourseNotFoundException, 
        *  CourseUpdateException 
        * to handle in struts-config.xml as a global exception
      *********/
    public ActionForward view(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
                         throws CourseNotFoundException, CourseUpdateException {

        System.out.println("CourseManageAction: view()");

         /** WORKSHOP W06_CONTROLLER
           * Get the course_application_id from the HttpServletRequest
          **********/
        String courseId = request.getParameter("course_id");

        /** WORKSHOP W06_CONTROLLER
          * Find the details of the course application using the appId
          * Use the findCourse() method of the CourseManager class
          **********/
        CourseManager courseMgr = new CourseManager();
        Course course = courseMgr.findCourse(courseId);


        /** WORKSHOP W06_CONTROLLER
          * Transfer the course application info to the form bean using
          * BeanUtils.copyProperties() method
          **********/
        CourseForm courseForm = (CourseForm) form;
        try {
            BeanUtils.copyProperties(courseForm, course);
        } catch (Exception e) {
            System.out.println("Error converting to form bean: " + e);
            throw new CourseUpdateException(
                "Could not populate the form bean: " + e);
        }
        System.out.println("CourseManageAction: view() successful");

        //Set FORM_SUBMIT_URL to /protected/courseUpdate.do
        request.setAttribute(
            "FORM_SUBMIT_URL",
            "protected/courseUpdate.do");
            
        /** WORKSHOP W06_CONTROLLER
          * Forward to a mapping by looking up the value "updateForm"
          **********/
        return mapping.findForward("updateForm");
    }

    /**
      * The withdraw method gets a course_id, and sets the status
      * of the course appropriately.
      */
    public ActionForward withdraw(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
                         throws CourseNotFoundException {

        System.out.println("CourseManageAction: withdraw()");

        // Get the modifier id (current user's employees id)

        HttpSession session = request.getSession();
        UserSession userSession =
            (UserSession) session.getAttribute("USERSESSION");
        String modifierId = userSession.getEmployee().getEmployeeId();

        // Delegate the operation to the Business Delegate

        CourseManager courseMgr = new CourseManager();
        String courseId = request.getParameter("course_id");
        courseMgr.withdrawCourse(courseId, modifierId, "Withdrawn by employee");

        // If no exception was thrown, go to the default page

        System.out.println("CourseManageAction: withdraw() successful");
        return mapping.findForward("employeeDefault");
    }

}